/*
 * @Description:
 * @Author: maskMan
 * @LastEditTime: 2024-03-20 20:21:34
 */
import { _decorator, Component, Node, Vec3, v3 } from "cc";
import { bordManger } from "./bordManger";
import { Constants } from "./utils/constants";
const { ccclass, property } = _decorator;

@ccclass("cameraCtrl")
export class cameraCtrl extends Component {
  @property(bordManger)
  bordManger: bordManger = null;
  @property(Node)
  panel: Node = null;
  speedX = 4;
  speedY = 7;
  start() {}
  _tempPos: Vec3 = v3();
  _ballPos: Vec3 = v3();
  ballPosY: number = null;
  panelPos: Vec3 = v3();
  update(dt: number) {
    if (this.bordManger.currBall) {
      this.node.getPosition(this._tempPos);
      this.bordManger.currBall.node.getPosition(this._ballPos);
      if (this._tempPos.x > this._ballPos.x) {
        const diffValue = Math.abs(Math.abs(this._tempPos.x) - Math.abs(this._ballPos.x));
        if (diffValue > 0.1) {
          this._tempPos.x -= this.speedX * dt;
        }
      } else if (this._tempPos.x < this._ballPos.x) {
        const diffValue = Math.abs(Math.abs(this._tempPos.x) - Math.abs(this._ballPos.x));
        if (diffValue > 0.1) {
          this._tempPos.x += this.speedX * dt;
        }
      }
      this.ballPosY = this.bordManger.currBall.lastPosY + 3;
      if (this._tempPos.y > this.ballPosY) {
        const diffValue = Math.abs(Math.abs(this._tempPos.y) - Math.abs(this.ballPosY));
        if (diffValue > 0.1) {
          this._tempPos.y -= this.speedY * dt;
        }
      } else if (this._tempPos.y < this.ballPosY) {
        const diffValue = Math.abs(Math.abs(this._tempPos.y) - Math.abs(this.ballPosY));
        if (diffValue > 0.1) {
          this._tempPos.y += this.speedY * dt;
        }
      }
      this.panel.getPosition(this.panelPos);
      this.panelPos.y = this._tempPos.y;
      this.panel.setPosition(this.panelPos);
      this.node.setPosition(this._tempPos);
    }
  }
}
